﻿Imports TransportadoraToledoDAL
Public Class CargasBLL

#Region "Propriedades"
    Private carCodigo As Integer
    Public Property pCarCodigo() As Integer
        Get
            Return carCodigo
        End Get
        Set(ByVal value As Integer)
            carCodigo = value
        End Set
    End Property

   
    Private carValor As Double
    Public Property pcarValor() As Double
        Get
            Return carValor
        End Get
        Set(ByVal value As Double)
            carValor = value
        End Set
    End Property

    Private tcCodigo As Integer
    Public Property pTcCodigo() As Integer
        Get
            Return tcCodigo
        End Get
        Set(ByVal value As Integer)
            tcCodigo = value
        End Set
    End Property

    Private segCodigo As Integer
    Public Property pSegCodigo() As Integer
        Get
            Return segCodigo
        End Get
        Set(ByVal value As Integer)
            segCodigo = value
        End Set
    End Property

    Private protocolo As String
    Public Property pProtocolo() As String
        Get
            Return protocolo
        End Get
        Set(ByVal value As String)
            protocolo = value
        End Set
    End Property

    Private carStatus As Integer
    Public Property pCarStatus() As Integer
        Get
            Return carStatus
        End Get
        Set(ByVal value As Integer)
            carStatus = value
        End Set
    End Property


    Private peso As Double
    Public Property pPeso() As Double
        Get
            Return peso
        End Get
        Set(ByVal value As Double)
            peso = value
        End Set
    End Property

    Private Verifica As Boolean
    Public Property pVerifica() As Boolean
        Get
            Return Verifica
        End Get
        Set(ByVal value As Boolean)
            Verifica = value
        End Set
    End Property
    Private lCargas As List(Of Cargas_AgendamentoBLL)
    Public Property pLCargas() As List(Of Cargas_AgendamentoBLL)
        Get
            Return lCargas
        End Get
        Set(ByVal value As List(Of Cargas_AgendamentoBLL))
            lCargas = value
        End Set
    End Property

#End Region

#Region "Construtores"

    'por favor, amanha se possivel crie ou modifique o método que procura pelo código por este
    'select c.car_codigo,c.car_valor,t.tc_descricao,s.seg_nome,c.car_status,c.car_peso
    'from cargas c
    '	inner join tipos_cargas t ON t.tc_codigo = c.tc_codigo
    '	inner join seguro s on s.seg_codigo = c.seg_codigo
    'preciso pegar a descrição do tipo da carga e o seu não retorna
    'att Willian Dias

    Sub New()

    End Sub

    Sub New(ByVal carCodigo As Integer, ByVal carValor As Double, ByVal tcCodigo As Integer,
            ByVal segCodigo As Integer, ByVal Peso As Double)
        Me.carCodigo = carCodigo
        Me.carValor = carValor
        Me.tcCodigo = tcCodigo
        Me.segCodigo = segCodigo
        Me.peso = Peso
    End Sub

#End Region

#Region "Métodos"

    Sub inserir()
        Dim objDAL As New CargasDAL
        Try
            If (tcCodigo = 0) Then
                Throw New Exception("Defina o tipo da carga")
            ElseIf (segCodigo = 0) Then
                Throw New Exception("Defina o tipo de seguro da carga")
            ElseIf (carValor = 0) Then
                Throw New Exception("Defina o valor da carga")
            ElseIf (peso = 0) Then
                Throw New Exception("Defina o peso da carga")
            Else
                Me.carCodigo = objDAL.inserir(carValor, tcCodigo, segCodigo, protocolo, carStatus, peso)
            End If
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
    End Sub

    Sub alterar()
        Dim objDAL As New CargasDAL
        Try
            If (carValor = 0) Then
                Throw New Exception("Defina o valor da carga")
            ElseIf (tcCodigo = 0) Then
                Throw New Exception("Defina o tipo da carga")
            ElseIf (segCodigo = 0) Then
                Throw New Exception("Defina o tipo de seguro da carga")
            ElseIf (peso = 0) Then
                Throw New Exception("Defina o peso da carga")
            Else
                objDAL.alterar(carCodigo, carValor, tcCodigo, segCodigo, protocolo, carStatus, peso)
            End If
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
    End Sub



    Sub excluir()
        Dim objDAL As New CargasDAL
        Try
            objDAL.excluir(carCodigo)
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
    End Sub

    Function localizarProtocolo(ByVal descricao As String) As DataTable
        Dim tab As DataTable
        Dim objDAL = New CargasDAL
        Try
            tab = objDAL.localizarProtocolo(descricao)
            objDAL = Nothing
            Return tab
        Catch ex As Exception
            Throw ex
        End Try
    End Function

    Function localizar(ByVal codigo As Integer) As DataTable
        Dim tab As DataTable
        Dim objDAL = New CargasDAL
        Try
            tab = objDAL.localizar(codigo)
            If (tab.Rows.Count > 0) Then
                Me.carCodigo = tab.Rows(0)(0).ToString
                Me.carValor = tab.Rows(0)(1).ToString
                Me.tcCodigo = tab.Rows(0)(2).ToString
                Me.segCodigo = tab.Rows(0)(3).ToString
                Me.protocolo = tab.Rows(0)(4).ToString
                Me.carStatus = tab.Rows(0)(5).ToString
                Me.peso = tab.Rows(0)(6).ToString
            Else
                limpar()
            End If
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
        Return tab
    End Function
    'retorna todas as linhas pesquisadas da tabela
    Function localizarPrimeiroUltimo(ByVal nome As String) As Integer
        Dim tab As DataTable = Nothing
        Dim codigo As Integer = 0
        Dim objDAL = New CargasDAL

        Try
            If (nome = "ultimo") Then
                objDAL = New CargasDAL
                tab = objDAL.localizar(nome)
                If (tab.Rows(0)("car_codigo").ToString <> "") Then
                    codigo = tab.Rows(0)("car_codigo").ToString
                End If
            ElseIf (nome = "primeiro") Then
                objDAL = New CargasDAL
                tab = objDAL.localizar(nome)
                If (tab.Rows(0)("car_codigo").ToString <> "") Then
                    codigo = tab.Rows(0)("car_codigo").ToString
                End If
            End If


        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
        Return codigo
    End Function

    Function localizarProxAnteriro(ByVal nome As String, ByVal codigo As Integer) As Integer
        Dim tab As DataTable = Nothing
        Dim CodigoP As Integer = 0
        Dim objDAL = New CargasDAL
        Try
            tab = objDAL.localizarProxAnterior(nome, codigo)
            If (tab.Rows.Count > 0) Then
                CodigoP = tab.Rows(0)("car_codigo").ToString
            End If


        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
        Return CodigoP
    End Function

    Sub limpar()
        Me.carCodigo = Nothing
        Me.carValor = Nothing
        Me.peso = Nothing
        Me.protocolo = Nothing
        Me.segCodigo = Nothing
        Me.carStatus = Nothing
        Me.tcCodigo = Nothing
    End Sub

    Sub VerificaExclusao(ByVal codigo As Integer)
        Try

            Dim objDAL As CargasDAL
            objDAL = New CargasDAL
            Me.Verifica = objDAL.VerificaExclusao(codigo)

            objDAL = Nothing

        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Sub VerificaMovimentacao(ByVal codigo As Integer)
        Try

            Dim objDAL As CargasDAL
            objDAL = New CargasDAL
            Me.Verifica = objDAL.VerificaMovimentacao(codigo)

            objDAL = Nothing

        Catch ex As Exception
            Throw ex
        End Try
    End Sub

#End Region
End Class
